import request from '@/utils/request'

export function getOrganizationUnits() {
  return request({
    url: `/api/identity/organization-units`,
    method: 'get'
  })
}

export function getOrganizationUnit(id) {
  return request({
    url: `/api/identity/organization-units/${id}`,
    method: 'get'
  })
}

export function getOrganizationUnitForEdit(id) {
  return request({
    url: `/api/identity/organization-units/${id}/for-edit`,
    method: 'get'
  })
}

export function createOrganizationUnit(data) {
  return request({
    url: `/api/identity/organization-units`,
    method: 'post',
    data
  })
}

export function updateOrganizationUnit(id, data) {
  return request({
    url: `/api/identity/organization-units/${id}`,
    method: 'put',
    data
  })
}

export function moveOrganizationUnit(id, data) {
  return request({
    url: `/api/identity/organization-units/${id}/move`,
    method: 'put',
    data
  })
}

export function deleteOrganizationUnit(id) {
  return request({
    url: `/api/identity/organization-units/${id}`,
    method: 'delete'
  })
}

export function getOrganizationUnitUserPagedList(id, query) {
  return request({
    url: `/api/identity/organization-units/${id}/members`,
    method: 'get',
    params: query
  })
}

export function getUserPagedListForAdd(organizationUnitId, query) {
  return request({
    url: `/api/identity/organization-units/${organizationUnitId}/members/search`,
    method: 'get',
    params: query
  })
}

export function addUserToOrganizationUnit(id, data) {
  return request({
    url: `/api/identity/organization-units/${id}/members`,
    method: 'put',
    data
  })
}

export function removeUserFromOrganizationUnit(organizationUnitId, userId) {
  return request({
    url: `/api/identity/organization-units/${organizationUnitId}/members/${userId}`,
    method: 'delete'
  })
}

export function getOrganizationUnitRolePagedList(id, query) {
  return request({
    url: `/api/identity/organization-units/${id}/roles`,
    method: 'get',
    params: query
  })
}

export function getRolePagedListForAdd(organizationUnitId, query) {
  return request({
    url: `/api/identity/organization-units/${organizationUnitId}/roles/search`,
    method: 'get',
    params: query
  })
}

export function addRoleToOrganizationUnit(id, data) {
  return request({
    url: `/api/identity/organization-units/${id}/roles`,
    method: 'put',
    data
  })
}

export function removeRoleFromOrganizationUnit(organizationUnitId, roleId) {
  return request({
    url: `/api/identity/organization-units/${organizationUnitId}/roles/${roleId}`,
    method: 'delete'
  })
}

// todo
export function getOrganizationUnitsWithLevel() {
  return request({
    url: '/api/services/app/OrganizationUnit/GetOrganizationUnitsWithLevelAsync',
    method: 'get'
  })
}
